package com.cnap.multiclouldmgmt.service.impl;

import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.enums.CloudVendorsEnum;
import com.cnap.multiclouldmgmt.model.k8sModels.EventModel;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterBasicModel;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterModel;
import com.cnap.multiclouldmgmt.model.vm.CheckDelVmModel;
import com.cnap.multiclouldmgmt.model.vm.VmCreateModel;
import com.cnap.multiclouldmgmt.model.vm.VmModel;
import com.cnap.multiclouldmgmt.repository.master.CnapKeyValueMapper;
import com.cnap.multiclouldmgmt.repository.master.K8sClusterEventsMapper;
import com.cnap.multiclouldmgmt.repository.master.K8sClusterInfoMapper;
import com.cnap.multiclouldmgmt.service.*;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import io.fabric8.kubernetes.api.model.EventSource;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.api.model.events.v1beta1.Event;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;

class MultiCloudMgmtServiceImplTest extends MulticlouldmgmtApplicationTests {

    @Autowired
    private MultiCloudMgmtService multiCloudMgmtService;

    @MockBean
    private K8sClusterInfoMapper k8sClusterInfoMapper;

    @MockBean
    private CacheService cacheService;

    @MockBean
    private K8sClusterEventsMapper k8sClusterEventsMapper;

    @MockBean
    private DockerHarborService dockerHarborService;

    @MockBean
    private K8sClusterService k8sClusterService;

    @MockBean
    private CnapSshService cnapSshService;

    @MockBean
    private VmResourceService vmResourceService;

    @MockBean
    private CnapKeyValueMapper cnapKeyValueMapper;

    /**
     * 存在的集群ip，1.1.1.1
     * 不存在 2.2.2.2
     * 集群名称 clusterName
     * 云统一用 aliyun
     */
    private void setMockData(){

        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModel.setK8sClusterIp("1.1.1.1");
        k8sClusterModel.setK8sClusterName("clusterName");
        k8sClusterModel.setCloudVendor("aliyun");
        k8sClusterModel.setK8sClusterLocation("js");
        k8sClusterModel.setCreateUser(CnapCloudMgmtConstants.TEST_ADMIN);
        List<VmModel> vmModelList = new ArrayList<>();
        VmModel vmModel = new VmModel();
        vmModel.setRegionId("region-id");
        vmModel.setZoneId("zone-id");
        vmModel.setBusinessId("biz-id");
        vmModel.setCloudType("Aliyun");
        vmModel.setExternalIp("3.3.3.3");
        vmModel.setName("vmName");
        vmModel.setVmId("vmId");
        vmModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_MASTER);
        vmModelList.add(vmModel);
        k8sClusterModel.setVmModels(vmModelList);
        List<K8sClusterModel> k8sClusterModels = new ArrayList<>();
        k8sClusterModels.add(k8sClusterModel);


        //cacheService
        Mockito.when(cacheService.getK8sClient("2.2.2.2"))
                .thenReturn(null);
        Mockito.when(cacheService.getK8sClient("1.1.1.1"))
                .thenReturn(null);

        //k8sClusterInfoMapper
        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByName("clusterNameNo"))
                .thenReturn(null);
        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByIp("2.2.2.2"))
                .thenReturn(null);
        Mockito.when(k8sClusterInfoMapper
                .countK8sClusterByCloudVendorOrName("aliyun",
                        "clusterName","admin",null)).thenReturn(1);
        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByName("clusterName"))
                .thenReturn(k8sClusterModel);
        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByIp("1.1.1.1"))
                .thenReturn(k8sClusterModel);
        Mockito.when(k8sClusterInfoMapper
                .countK8sClusterByCloudVendorOrName("aliyun",
                        "clusterName",CnapCloudMgmtConstants.TEST_ADMIN,null)).thenReturn(1);
        Mockito.when(k8sClusterInfoMapper.getK8sClusterByCloudVendorOrNameByPage(
                "aliyun", "clusterName",10,0,
                CnapCloudMgmtConstants.TEST_ADMIN,null)).thenReturn(k8sClusterModels);
        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByZone(
                getZoneId(vmModel,CnapCloudMgmtConstants.TEST_ADMIN)))
                .thenReturn(k8sClusterModel);
        Mockito.when(k8sClusterInfoMapper.getAllK8sClusterByClouderVendor("aliyun"))
                .thenReturn(k8sClusterModels);


        Mockito.when(cnapKeyValueMapper.getKvByKey("black")).thenReturn("hello");

    }

    private String getZoneId(VmModel vmModel, String userName){

        return vmModel.getCloudType() + "+"
                + vmModel.getRegionId() + "+"
                + vmModel.getZoneId() + "+"
                + userName;
    }

    @Test
    void tstAddK8sCluster(){
        try {
            K8sClusterModel k8sClusterModel = new K8sClusterModel();
            k8sClusterModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
            Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByName(CnapCloudMgmtConstants.TEST_ADMIN))
                    .thenReturn(k8sClusterModel);
            multiCloudMgmtService.addK8sCluster(k8sClusterModel);
        }catch (Exception e){

        }

        try {
            K8sClusterModel k8sClusterModel = new K8sClusterModel();
            k8sClusterModel.setK8sClusterName(CnapCloudMgmtConstants.TEST_ADMIN);
            k8sClusterModel.setK8sClusterIp(CnapCloudMgmtConstants.TEST_ADMIN);
            Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByIp(CnapCloudMgmtConstants.TEST_ADMIN))
                    .thenReturn(k8sClusterModel);
            multiCloudMgmtService.addK8sCluster(k8sClusterModel);

        }catch (Exception e){

        }


    }

    @Test
    void addK8sCluster() throws Exception {

        setMockData();

        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModel.setK8sClusterName("clusterNameNo");
        k8sClusterModel.setK8sClusterIp("2.2.2.2");
        k8sClusterModel.setCreateUser(CnapCloudMgmtConstants.TEST_ADMIN);
        k8sClusterModel.setK8sClusterConfig("test");

        k8sClusterModel.setCloudVendor(CloudVendorsEnum.TencentCloud.getDescribe());
        multiCloudMgmtService.addK8sCluster(k8sClusterModel);

        k8sClusterModel.setCloudVendor(CloudVendorsEnum.Aliyun.getDescribe());
        multiCloudMgmtService.addK8sCluster(k8sClusterModel);

        k8sClusterModel.setCloudVendor(CloudVendorsEnum.HuaWeiCloud.getDescribe());
        multiCloudMgmtService.addK8sCluster(k8sClusterModel);

    }

    @Test
    void getK8sClusterNum() {
        setMockData();
        multiCloudMgmtService.getK8sClusterNum("aliyun",
                "clusterName","admin");
        multiCloudMgmtService.getK8sClusterNum("aliyun",
                "clusterName");
        EventModel eventModel = new EventModel();
    }

    @Test
    void testGetK8sClusterNum() {
    }

    @Test
    void getK8sClusterIpByName() {
        multiCloudMgmtService.getK8sClusterIpByName("clusterName");
        multiCloudMgmtService.getK8sClusterInfoByName("clusterNameNo");
    }

    @Test
    void getK8sClusterInfoByName() {

        setMockData();
        multiCloudMgmtService.getK8sClusterInfoByName("clusterName");
    }

    @Test
    void getK8sClusterInfos() {
        setMockData();
        multiCloudMgmtService.getK8sClusterInfos("aliyun",
                "clusterName",10,1,CnapCloudMgmtConstants.TEST_ADMIN);
    }

    @Test
    void testGetK8sClusterInfos() {
    }

    @Test
    void editK8sClusterInfo() throws Exception {
        setMockData();
        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModel.setK8sClusterIp("1.1.1.1");
        k8sClusterModel.setK8sClusterName("clusterName");
        multiCloudMgmtService.editK8sClusterInfo(k8sClusterModel);

        Event resource = new Event();
        resource.setRegarding(new ObjectReference());
        resource.setMetadata(new ObjectMeta());
        resource.setDeprecatedSource(new EventSource());
        

        EventModel eventModel =
                multiCloudMgmtService.transResourceToEventModel(resource,k8sClusterModel,CnapCloudMgmtConstants.TEST_ADMIN);
        multiCloudMgmtService.putK8sEventToDb(eventModel);
        
    }

    @Test
    void delK8sCluster() throws Exception {
        setMockData();
        multiCloudMgmtService.delK8sCluster("1.1.1.1");
    }

    @Test
    void getK8sClientByIp() throws Exception {

        setMockData();
        multiCloudMgmtService.getK8sClientByIp("1.1.1.1");
    }

    @Test
    void getK8sClientByCloudVendorAndName() throws Exception {
        setMockData();
        multiCloudMgmtService.getK8sClientByCloudVendorAndName("aliyun",
                "clusterName");
    }

    @Test
    void getCloudVendorsByCreateUser() {

        multiCloudMgmtService.getCloudVendorsByCreateUser("user");
    }

    @Test
    void getK8sIpsByUserName() {

        multiCloudMgmtService.getK8sIpsByUserName("user");

    }

    @Test
    void getAllK8sClusterIps() {
        multiCloudMgmtService.getAllK8sClusterIps();
    }

    @Test
    void getAllK8sClusterNames() {
        multiCloudMgmtService.getAllK8sClusterNames();
    }

    @Test
    void getK8sClusterModelByVmCreateModel() {
        setMockData();
        VmCreateModel vmCreateModel = new VmCreateModel();
        vmCreateModel.setUserName(CnapCloudMgmtConstants.TEST_ADMIN);
        vmCreateModel.setCategory("ai");

        List<VmModel> vmModelList = new ArrayList<>();
        VmModel vmModelNo = new VmModel();
        vmModelNo.setRegionId("region-idNo");
        vmModelNo.setZoneId("zone-idNo");
        vmModelNo.setBusinessId("biz-idNo");
        vmModelNo.setCloudType("AliyunNo");
        vmModelNo.setExternalIp("4.4.4.4");
        vmModelNo.setName("vmNameNo");
        vmModelNo.setCategory("ai");
        vmModelList.add(vmModelNo);
        vmCreateModel.setVmModels(vmModelList);

        multiCloudMgmtService.getK8sClusterModelByVmCreateModel(vmCreateModel);

        VmModel vmModel = new VmModel();
        vmModel.setRegionId("region-id");
        vmModel.setZoneId("zone-id");
        vmModel.setBusinessId("biz-id");
        vmModel.setCloudType("Aliyun");
        vmModel.setExternalIp("3.3.3.3");
        vmModel.setName("vmName");
        vmModel.setCategory("ai");
        vmCreateModel.setVmModels(new ArrayList<>());
        vmCreateModel.getVmModels().add(vmModel);
        vmCreateModel.getVmModels().add(vmModelNo);
        multiCloudMgmtService.getK8sClusterModelByVmCreateModel(vmCreateModel);

    }

    @Test
    void installK8sFramwork() throws Exception {

        setMockData();
        VmCreateModel vmCreateModel = new VmCreateModel();
        vmCreateModel.setUserName(CnapCloudMgmtConstants.TEST_ADMIN);
        vmCreateModel.setCategory("ai");

        List<VmModel> vmModelList = new ArrayList<>();
        VmModel vmModelNo = new VmModel();
        vmModelNo.setRegionId("region-idNo");
        vmModelNo.setZoneId("zone-idNo");
        vmModelNo.setBusinessId("biz-idNo");
        vmModelNo.setCloudType("AliyunNo");
        vmModelNo.setExternalIp("4.4.4.4");
        vmModelNo.setName("vmNameNo");
        vmModelNo.setCategory("ai");
        vmModelList.add(vmModelNo);
        VmModel vmModelNo2 = new VmModel();
        vmModelNo2.setRegionId("region-idNo");
        vmModelNo2.setZoneId("zone-idNo");
        vmModelNo2.setBusinessId("biz-idNo");
        vmModelNo2.setCloudType("AliyunNo");
        vmModelNo2.setExternalIp("4.4.4.4");
        vmModelNo2.setName("vmNameNo");
        vmModelNo2.setCategory("ai");
        vmModelList.add(vmModelNo2);
        vmCreateModel.setVmModels(vmModelList);

        multiCloudMgmtService.installK8sFramwork(vmCreateModel);

        VmModel vmModel = new VmModel();
        vmModel.setRegionId("region-id");
        vmModel.setZoneId("zone-id");
        vmModel.setBusinessId("biz-id");
        vmModel.setCloudType("Aliyun");
        vmModel.setExternalIp("3.3.3.3");
        vmModel.setName("vmName");
        vmModel.setCategory("ai");
        vmCreateModel.setVmModels(new ArrayList<>());
        vmCreateModel.getVmModels().add(vmModel);
        vmCreateModel.getVmModels().add(vmModelNo);
        multiCloudMgmtService.installK8sFramwork(vmCreateModel);
    }

    @Test
    void installK8sWorker() throws Exception {
        VmModel vmModel = new VmModel();
        VmModel masterModel = new VmModel();
        masterModel.setCloudType(CloudVendorsEnum.TencentCloud.getCloudType());
        multiCloudMgmtService.installK8sWorker(CnapCloudMgmtConstants.TEST_ADMIN,
                vmModel,masterModel,"ai",new VmCreateModel());
        multiCloudMgmtService.getK8sClusterInfosByZone(vmModel,CnapCloudMgmtConstants.TEST_ADMIN);
    }

    @Test
    void testInstallK8sWorker() {
    }

    @Test
    void getK8sClusterModelByZoneId() {
        VmCreateModel vmCreateModel = new VmCreateModel();
        VmModel vmModel = new VmModel();
        vmModel.setRegionId("region-id");
        vmModel.setZoneId("zone-id");
        vmModel.setBusinessId("biz-id");
        vmModel.setCloudType("Aliyun");
        vmModel.setExternalIp("3.3.3.3");
        vmModel.setName("vmName");
        vmModel.setCategory("ai");
        vmCreateModel.setVmModels(new ArrayList<>());
        vmCreateModel.getVmModels().add(vmModel);
        multiCloudMgmtService.getK8sClusterModelByZoneId(vmCreateModel);
    }

    @Test
    void updateVmJson() {

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterIp("1.1.1.1");
        multiCloudMgmtService.updateVmJson(k8sClusterBasicModel,"test");
    }

    @Test
    void checkVmDelResult() {
        setMockData();
        CheckDelVmModel checkDelVmModel = new CheckDelVmModel();
        checkDelVmModel.setCloudType("Aliyun");
        checkDelVmModel.setVmId("vmId");
        CheckDelVmModel checkDelVmModel1 = multiCloudMgmtService.checkVmDelResult(checkDelVmModel);

        CheckDelVmModel checkDelVmModelNo = new CheckDelVmModel();
        checkDelVmModelNo.setCloudType("Huawei");
        multiCloudMgmtService.checkVmDelResult(checkDelVmModelNo);


    }

    @Test
    void testInstallK8s() throws Exception {
        VmCreateModel vmCreateModel = new VmCreateModel();
        vmCreateModel.setUserName(CnapCloudMgmtConstants.TEST_ADMIN);
        vmCreateModel.setVmModels(new ArrayList<>());
        VmModel vmModel = new VmModel();
        vmCreateModel.getVmModels().add(vmModel);
        vmModel.setBusinessId("bizid");

        List<K8sClusterModel> k8sClusterModels = new ArrayList<>();
        K8sClusterModel k8sClusterModel = new K8sClusterModel();
        k8sClusterModels.add(k8sClusterModel);
        k8sClusterModel.setVmModels(new ArrayList<>());
        VmModel oldMasterModel = new VmModel();
        k8sClusterModel.getVmModels().add(oldMasterModel);
        oldMasterModel.setBusinessId("bizid");
        oldMasterModel.setK8sCluterRole(CnapCloudMgmtConstants.CNAP_VM_MASTER);

        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfosByZone(
                Mockito.anyString())).thenReturn(k8sClusterModels);
        K8sClusterModel k8sClusterModel01 = new K8sClusterModel();
        k8sClusterModel01.setVmModels(new ArrayList<>());
        k8sClusterModel01.setK8sClusterName("aaa");

        Mockito.when(k8sClusterInfoMapper.getK8sClusterInfoByName(Mockito.anyString()))
                        .thenReturn(k8sClusterModel01);

        multiCloudMgmtService.installK8sFramwork(vmCreateModel);
    }
}